package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.entity.Address;
import com.shop.mapper.AddressMapper;
import com.shop.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author D
 * @since 2023-03-11 03:01:17
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Autowired
    AddressMapper addressMapper;
    /**
     * 添加地址
     * @param address 用户地址信息
     * @param userId 用户ID
     */
    @Override
    public void addAddress(Address address, Integer userId) {
        final String isDefault = "1";
        final String isNotDefault = "0";
        address.setUserId(userId);
        if (isDefault.equals(address.getIsDefault())) {
            Address defaultAddress = new Address();
            defaultAddress.setIsDefault(isNotDefault);
            QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("userId", userId);
            addressMapper.update(defaultAddress, queryWrapper);
        }
        addressMapper.insert(address);
    }
    /**
     * 根据用户ID获取用户地址列表
     * @param userId 用户ID
     * @return 用户地址列表
     */
    @Override
    public List<Address> getListByUserId(Integer userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        return addressMapper.selectList(queryWrapper);
    }
}
